package com.chris.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.chris.common.dao.UserMapper;
import com.chris.common.entity.User;
import com.chris.entity.SecurityUser;
import com.chris.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public SecurityUser loadByUsername(String username) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("username", username);
        List<User> list = userMapper.selectList(wrapper);
        SecurityUser securityUser = null;
        if(CollectionUtils.isNotEmpty(list)){
            User user = list.get(0);
            //角色
            List<String> roles = new ArrayList<>();
            roles.add("ROLE_ADMIN");
            //具体权限
            List<String> authoritys = new ArrayList<>();
            roles.add("queryHello");
            List<String> allPerm = new ArrayList<>();
            allPerm.addAll(roles);
            allPerm.addAll(authoritys);
            securityUser = new SecurityUser(user.getUsername(),user.getPassword(),allPerm);
        }
        return securityUser;
    }
}
